// pages/categories/categories.js
var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goods: [],
        goodslist: [

        ],
        categories_tab: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCat();
    },
    getCat: function () {
        var that = this;
        var data = {
            url: "/wsgoods/goodscat/getcattree",
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {

                    data[index].pic = c.img_url + data[index].pic;
                }
                data.forEach((item, index) => {
                    item.child_list.forEach((v1, i1) => {
                        if (v1.child_list.length >0) {
                            v1.child_list.forEach((v2, i2) => {
                                data[index].child_list[i1].child_list[i2].pic = c.img_url + v2.pic;
                            });
                        }else{
                            data[index].child_list[i1].pic = c.img_url + v1.pic;
                        }

                    });
                });

                that.setData({
                    goods: data
                });
            } else {

            }
        });
    },
    categories_tab: function (e) {
        let key = e.currentTarget.dataset.select;
        this.setData({
            categories_tab: key
        })
    },
    //分类图片错误处理
    errCategoryPic: function (e) {
        var index = e.target.dataset.index;
        var goods = this.data.goods;
        var arr = new Array(); //定义一数组 
        arr = index.split("-");
        if(arr[2]){
            goods[arr[0]].child_list[arr[1]].child_list[arr[2]].pic = "/static/images/shop/default.png";
        }else{
            goods[arr[0]].child_list[arr[1]].pic = "/static/images/shop/default.png";
        }
        
        this.setData({
            goods: goods
        });
    },
})